#include "instruction.h"

static word memory [INSTRUCTION_WORDS];
static unsigned int memory_index = 0;

int load_instruction (word *buf, int size)
{
	if (size + memory_index > INSTRUCTION_WORDS)
	{
		return -1;
	}
	memcpy (memory + memory_index, buf, size * sizeof (word));
	memory_index = memory_index + size;
	return 0;
}

int clear_instruction (void)
{
	memory_index = 0;
	return 0;
}

word fetch (word address)
{
	address = address & INSTRUCTION_MASK;
	return memory [address];
}

void decode (const word instruction, word *opcode, word *creg, word *areg, word *breg, word *im22, word *im18)
{
	*opcode = instruction >> 26;
	*creg = (instruction >> 22) & 0xF;
	*areg = (instruction >> 18) & 0xF;
	*breg = (instruction >> 14) & 0xF;
	*im22 = instruction & 0x3FFFFF;
	*im18 = instruction & 0x3FFFF;
}
